package org.itboys.weixin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.fuzion.dto.FuzionMemberDto;
import org.itboys.fuzion.dto.FuzionMemberRedisDto;
import org.itboys.fuzion.entity.FuzionMember;
import org.itboys.fuzion.service.FuzionMemberService;
import org.itboys.manager.MemberCoreService;
import org.itboys.weixin.tools.WeixinUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 微信会员中心
 * @author weisky
 *
 * 2016年4月18日
 */
@RestController 
@RequestMapping("/weixin/member") 
public class WeixinMemberController extends BaseController{

	@Autowired
	private FuzionMemberService fuzionMemberService;
	@Autowired
	private MemberCoreService memberCoreService;
	
	/**
	 * 会员的个人中心
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/memberCore") 
	public ModelAndView toMemberCore(Model model, @RequestParam(required=false) Long mId,
			HttpServletRequest request, HttpServletResponse response){
		Long memberId = (Long) request.getSession().getAttribute("memberId");
		if(memberId == null){//如果之前没有存入session  则进入
			/*fuzionMemberService.setSessionMember(mId, request, response);
			memberId = mId;*/
			return new ModelAndView("/error");
		}else{
			//获取个人信息
			FuzionMemberDto fm = memberCoreService.getByMemberId(memberId);
			model.addAttribute("me", fm);
			
			//获取推荐人信息
			FuzionMemberRedisDto fmd = fuzionMemberService.getFuzionMemberRedisDto(memberId);
			model.addAttribute("lederName", fmd.getRecName());
			model.addAttribute("lederLogo", fmd.getRecHeadImgUrl());
			
			return new ModelAndView("/site/member");
		}
	}
	
	/**
	 * 检测会员是否已经购买过产品
	 * @param request
	 * @param response
	 */
	@RequestMapping("/checkBuy") 
	public void checkBuy(HttpServletRequest request, HttpServletResponse response){
		Long memberId = (Long) request.getSession().getAttribute("memberId");
		FuzionMember fm = fuzionMemberService.getById(memberId);
		if(fm.getReaded()){//已经购买过产品
			AjaxUtils.renderJson(response, "1");
		}else{
			AjaxUtils.renderJson(response, "0");
		}
	}

	/**
	 * 查看我的团队
	 * @param model
	 * @param request
	 * @param response
     * @return
     */
	@RequestMapping("/myTeam")
	public ModelAndView myTeam(Model model,
			HttpServletRequest request, HttpServletResponse response){
		Long memberId = (Long) request.getSession().getAttribute("memberId");
		// /获取我推荐的会员
		List<FuzionMember> myMeLi = fuzionMemberService.getMyNextMember(memberId);
		model.addAttribute("myMeLi", myMeLi);
		return new ModelAndView("/site/myteam");
	}


}
